package com.ktg.mes.rec.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktg.common.core.domain.APIResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.rec.domain.RecGrindRecord;
import com.ktg.mes.rec.service.IRecGrindRecordService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 研磨记录Controller
 *
 * @author byteco
 * @date 2022-10-31
 */
@RestController
@RequestMapping("/rec/recGrindRecord")
public class RecGrindRecordController extends BaseController
{
    @Autowired
    private IRecGrindRecordService recGrindRecordService;

    /**
     * 查询研磨记录列表
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:list')")
    @GetMapping("/list")
    public Result<IPage<RecGrindRecord>> list(RecGrindRecord recGrindRecord,
                                              @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                              HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询研磨记录列表",req,null);
        try {
            Page<RecGrindRecord> page = new Page<RecGrindRecord>(pageNo, pageSize);
            IPage<RecGrindRecord> pageList = recGrindRecordService.queryPageList(recGrindRecord,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出研磨记录列表
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:export')")
    @Log(title = "研磨记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecGrindRecord recGrindRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出研磨记录列表",req,recGrindRecord);
        try {
            List<RecGrindRecord> list = recGrindRecordService.queryList(recGrindRecord);
            ExcelUtil<RecGrindRecord> util = new ExcelUtil<RecGrindRecord>(RecGrindRecord.class);
            util.exportExcel(response, list, "研磨记录数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取研磨记录详细信息
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:query')")
    @GetMapping(value = "/{grindId}")
    public Result<RecGrindRecord> getInfo(@PathVariable("grindId") Long grindId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取研磨记录详细信息",req,null);
        try {
            return apiLog.ok(recGrindRecordService.getInfo(grindId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增研磨记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:add')")
    @Log(title = "研磨记录", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add(@RequestBody RecGrindRecord recGrindRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增研磨记录",req,recGrindRecord);
        try {
            recGrindRecordService.insert(recGrindRecord);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改研磨记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:edit')")
    @Log(title = "研磨记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<String> edit(@RequestBody RecGrindRecord recGrindRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改研磨记录",req,recGrindRecord);
        try {
            recGrindRecordService.update(recGrindRecord);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除研磨记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recGrindRecord:remove')")
    @Log(title = "研磨记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{grindIds}")
    public Result<String> remove(@PathVariable Long[] grindIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除研磨记录",req,null);
        try {
            recGrindRecordService.delete(grindIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
